
class Solution {
    static class TreeNode {
        char val;
        Demo.TreeNode left;
        Demo.TreeNode right;
        TreeNode(char val){
            this.val = val;
        }
    }
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        } 
        return getHeight(root)>=0;
    }
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int lefthight = getHeight(root.left);
        if(lefthight < 0) {
            return -1;
        }
        int righthight = getHeight(root.right);
        if(righthight < 0) {
            return -1;
        }
        if(Math.abs(lefthight-righthight)<=1){
        return lefthight>righthight?lefthight+1:righthight+1;
        }else{
            return -1;
        }
    }
}